const mysql = require('mysql')

const connection = mysql.createConnection({
	host: 'localhost',
	user: 'root',
	password: 'bao05083271',//bao584520
	database: 'hongshibao'
})

connection.connect(function(err) {
	if (err) {
		throw new Error('connect error')
		return
	}
	console.log('connect success');
})


//根据姓名查询用户
exports.findUserByname = function(user, password, callback) {
	const find = `SELECT * FROM users WHERE user='${user}' AND password='${password}'`;
	connection.query(find, function(err, result) {
		callback(err, result);
	})
}
//id
exports.findUserByid = function(id, callback) {
	const find = `SELECT * FROM users WHERE id=${id}`;
	connection.query(find, function(err, result) {
		callback(err, result);
	})
}

exports.register = function(user, password, type, callback) {
	const create = `INSERT INTO users (user, password, type) VALUES('${user}', '${password}', '${type}')`;
	connection.query(create, function(err, result) {
		callback(err, result);
	})
}

exports.updateUser = function(id, updateMsg, callback) {
	const { title, avatar, desc, company, money } = updateMsg; 
	console.log(updateMsg)
	const updates = `UPDATE users SET title='${title}',avatar='${avatar}',descmsg='${desc}',company='${company}',money='${money}' WHERE id=${id}`;
	console.log(updates)
	connection.query(updates, function(err, result) {
		callback(err, result);
	})
}